<?php
use common\Tools;
use common\components\wtools\tools\FileHelper;

if (!function_exists('dd')) {
    function dd(...$args)
    {
        if (function_exists('dump')) {
            dump(...$args);
        } else {
            var_dump(...$args);
        }
        exit;
    }
}

define("YII_PROJECT_ROOT", dirname(dirname(__DIR__))); // 根目录
/** @var array $bootstrap */
$bootstrap = require_once('bootstrap-local.php');

Yii::setAlias('@common', YII_PROJECT_ROOT.'/common');
Yii::setAlias('@home', YII_PROJECT_ROOT. '/home');
Yii::setAlias('@api', YII_PROJECT_ROOT . '/api');
Yii::setAlias('@console', YII_PROJECT_ROOT. '/console');

$base_url_head = Tools::getCurrentScheme();
if (isset($_SERVER['HTTP_ALI_SWIFT_STAT_HOST'])) { // cdn加速 阿里云
    $THIS_SERVER_BASE_URL = $base_url_head.$_SERVER['HTTP_ALI_SWIFT_STAT_HOST'];
    $_SERVER['HTTP_HOST'] = $_SERVER['SERVER_NAME'] = $_SERVER['HTTP_ALI_SWIFT_STAT_HOST'];
} else {
    if (Tools::isCli()){
        $THIS_SERVER_BASE_URL = $bootstrap['this_server_base_uri'];
    } else {
        $THIS_SERVER_BASE_URL = $base_url_head.$_SERVER['HTTP_HOST'];
    }
}

$THIS_SERVER_NAME = $bootstrap['this_server_name'];
$THIS_SERVER_IS_CROS = $bootstrap['this_server_is_cros'];

define("THIS_SERVER_BASE_URI", $THIS_SERVER_BASE_URL); // 域名地址
define("THIS_SERVER_NAME", $THIS_SERVER_NAME); // 应用名
define("THIS_SERVER_IS_CROS", $THIS_SERVER_IS_CROS); // 是否需要cros

Yii::setAlias('@wroot', YII_PROJECT_ROOT . '/web');
Yii::setAlias('@wurl', '/');
Yii::setAlias('@waburl', THIS_SERVER_BASE_URI);
Yii::setAlias('@common', dirname(__DIR__));

$appIds = ['api', 'home'];
foreach ($appIds as $k => $v) {
    Yii::setAlias("@{$v}", YII_PROJECT_ROOT. "/{$v}");
}
$subWebs = ['static', 'others', 'storage'];
foreach ($subWebs as $k => $v) {
    Yii::setAlias("@{$v}_root", YII_PROJECT_ROOT. "/web/{$v}");
    Yii::setAlias("@{$v}_url", "/{$v}");
    Yii::setAlias("@{$v}_aburl", $THIS_SERVER_BASE_URL. "/{$v}");
    if ($v === 'storage') {
        $mountDataFile = Yii::getAlias("@{$v}_root/mountData.json");
        if (!file_exists($mountDataFile)) {
            throw new \Exception("没有找到挂载文件！");
        } else {
            $mountData = file_get_contents($mountDataFile);
            $mountData = Tools::isJson($mountData);
            if (!$mountData['isMount']) {
                throw new \Exception("不允许挂载文件！");
            }
            if ($mountData['YII_ENV'] !== YII_ENV) {
                throw new \Exception("挂载文件环境不匹配！");
            }
            $upload_tmp_dir = Yii::getAlias("@{$v}_root/upload_tmp_dir");
            if (!is_dir($upload_tmp_dir)) {
                FileHelper::createDirectory($upload_tmp_dir, 0777);
//                throw new \Exception("临时上传目录缺失！");
            }
            ini_set('upload_tmp_dir', $upload_tmp_dir);
        }
        $subStorages = ['uploads', 'tmp'];
        foreach ($subStorages as $k1 => $v1) {
            Yii::setAlias("@{$v1}_root", YII_PROJECT_ROOT. "/web/{$v}/".YII_ENV."/{$v1}");
            Yii::setAlias("@{$v1}_url", "/{$v}/".YII_ENV."/{$v1}");
            Yii::setAlias("@{$v1}_aburl", $THIS_SERVER_BASE_URL. "/{$v}/".YII_ENV."/{$v1}");
            $dir = Yii::getAlias("@{$v1}_root");
            if (!is_dir($dir)) {
                FileHelper::createDirectory($dir, 0777);
            }
        }
    }
}

Yii::$classMap['yii\caching\Cache'] = '@common/rewrites/caching/Cache.php';
Yii::$classMap['moonland\phpexcel\Excel'] = '@common/rewrites/moonlandsoft/yii2-phpexcel/Excel.php';

Yii::$container->set(\yii\widgets\LinkPager::class, [
    'maxButtonCount' => 10,
    'firstPageLabel' => '首页',
    'lastPageLabel' => '末页',
    'prevPageLabel'=>'上一页',
    'nextPageLabel'=>'下一页',
]);

Yii::$container->set(\yii\data\Pagination::class, [
    'defaultPageSize' => 10,
]);
